package com.czl.module_service.view

import android.annotation.SuppressLint
import android.content.Context
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.czl.base.data.bean.tengyun.CategoryBean
import com.czl.module_base.adapter.CategoryAdapter
import com.czl.module_base.adapter.ListTreeAdapter
import com.czl.module_service.R
import com.lxj.xpopup.core.CenterPopupView

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
@SuppressLint("ViewConstructor")
open class CategoryPopup(context: Context, block: ((CategoryBean?) -> Unit)) :
    CenterPopupView(context) {

    private val mAdapter: CategoryAdapter by lazy {
        CategoryAdapter().apply {
            setOnItemClickListener(ListTreeAdapter.OnItemClickListener<CategoryBean> { t, _, _ ->
                dismiss()
                block(t)
            })
        }
    }

    override fun onCreate() {
        super.onCreate()
        initData()
    }

    private fun initData() {
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)

        recyclerView.run {
            adapter = mAdapter
        }
        val ivClose = findViewById<ImageView>(R.id.base_pop_close_iv)
        ivClose.setOnClickListener { dismiss() }
    }

    open fun setList(list: List<CategoryBean>) {
        mAdapter.addAll(list)
    }

    override fun getImplLayoutId(): Int {
        return R.layout.popup_category_storehouse
    }

    override fun getMaxHeight(): Int {
        return (ScreenUtils.getScreenHeight() * 0.6).toInt()
    }

    override fun getMaxWidth(): Int {
        return ScreenUtils.getScreenWidth() - SizeUtils.dp2px(30f)
    }
}